﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;
using System.Drawing;

namespace graduation_project.Voice_Recognition
{
    public partial class SimulationMain : Form
    {
        SimulationRecongnition simulationRecongnition = new SimulationRecongnition();
        //private string sstr = null;

        System.Windows.Forms.Timer time = new System.Windows.Forms.Timer();
        public SimulationMain()
        {
            InitializeComponent();
        }
        private void SimulationMain_Load(object sender, EventArgs e)
        {
            hideTextBox();
            hidePictureEdit();
            SimulationTextBox.Visible = false;
            SkinMiddle.sendEvent += SetSkin;
        }
        public void hideTextBox()
        {
            textBox1.Hide();
            textBox2.Hide();
            textBox3.Hide();
            textBox4.Hide();
            textBox5.Hide();
            textBox6.Hide();
            textBox7.Hide();
            textBox8.Hide();
        }
        public void hidePictureEdit()
        {
            pictureEdit1.Hide();
            pictureEdit2.Hide();
            pictureEdit3.Hide();
            pictureEdit4.Hide();
            pictureEdit5.Hide();
            pictureEdit8.Hide();
            pictureEdit6.Hide();
            pictureEdit7.Hide();

        }
        public void SwitchChange(PictureEdit picture, ToggleSwitch tswitch)
        {
            if (tswitch.IsOn)
            {
                picture.Show();
            }
            else
            {
                picture.Hide();
            }
        }

        private void toggleSwitch1_Toggled(object sender, EventArgs e)
        {
            SwitchChange(pictureEdit1, toggleSwitch1);
        }

        private void toggleSwitch2_Toggled(object sender, EventArgs e)
        {
            SwitchChange(pictureEdit2, toggleSwitch2);
        }

        private void toggleSwitch3_Toggled(object sender, EventArgs e)
        {
            SwitchChange(pictureEdit3, toggleSwitch3);
        }

        private void toggleSwitch4_Toggled(object sender, EventArgs e)
        {
            SwitchChange(pictureEdit4, toggleSwitch4);
        }

        private void toggleSwitch5_Toggled(object sender, EventArgs e)
        {
            SwitchChange(pictureEdit5, toggleSwitch5);
        }

        private void toggleSwitch6_Toggled(object sender, EventArgs e)
        {
            SwitchChange(pictureEdit6, toggleSwitch6);
        }

        private void toggleSwitch7_Toggled(object sender, EventArgs e)
        {
            SwitchChange(pictureEdit7, toggleSwitch7);
        }

        private void toggleSwitch8_Toggled(object sender, EventArgs e)
        {
            SwitchChange(pictureEdit8, toggleSwitch8);
        }
        //编辑label
        private void labelControl1_DoubleClick(object sender, EventArgs e)
        {
            textBox1.Text = labelControl1.Text;
            textBox1.Show();
        }

        private void labelControl2_DoubleClick(object sender, EventArgs e)
        {
            textBox2.Text = labelControl2.Text;
            textBox2.Show();
        }

        private void labelControl3_DoubleClick(object sender, EventArgs e)
        {
            textBox3.Text = labelControl3.Text;
            textBox3.Show();
        }

        private void labelControl4_DoubleClick(object sender, EventArgs e)
        {
            textBox4.Text = labelControl4.Text;
            textBox4.Show();
        }

        private void labelControl5_DoubleClick(object sender, EventArgs e)
        {
            textBox5.Text = labelControl5.Text;
            textBox5.Show();
        }

        private void labelControl6_DoubleClick(object sender, EventArgs e)
        {
            textBox6.Text = labelControl6.Text;
            textBox6.Show();
        }

        private void labelControl7_DoubleClick(object sender, EventArgs e)
        {
            textBox7.Text = labelControl7.Text;
            textBox7.Show();
        }

        private void labelControl8_DoubleClick(object sender, EventArgs e)
        {
            textBox8.Text = labelControl8.Text;
            textBox8.Show();
        }
        /// <summary>
        /// 编辑textbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelControl1.Text = textBox1.Text;
                textBox1.Hide();
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelControl2.Text = textBox2.Text;
                textBox2.Hide();
            }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelControl3.Text = textBox3.Text;
                textBox3.Hide();
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelControl4.Text = textBox4.Text;
                textBox4.Hide();
            }
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelControl5.Text = textBox5.Text;
                textBox5.Hide();
            }
        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelControl6.Text = textBox6.Text;
                textBox6.Hide();
            }
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelControl7.Text = textBox7.Text;
                textBox7.Hide();
            }
        }

        private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelControl8.Text = textBox8.Text;
                textBox8.Hide();
            }
        }
        /// <summary>
        /// 打开语音识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartSpeechRecongnitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //System.Windows.Forms.Timer time = new System.Windows.Forms.Timer();
            strClear();
            
            //time.Enabled = true;
            time.Interval = 10000;
            time.Start();
            //time.Tick += new EventHandler(time_Tick);
            time.Tick += new EventHandler(time_Tick);
        }

        void time_Tick(object sender, EventArgs e)
        {
            string str1 = null;
            string str2 = null;
            if (SimulationTextBox.Text == "打开电视机" || SimulationTextBox.Text == "打开客厅电视机")
            {
                str1 = "电视机";
                str2 = "打开";
                foreachLbl(str1, str2);
                #region 遍历lbl
                //foreach (Control lbl in this.groupBox1.Controls)
                //{
                //    //if (lbl.Text == str1)
                //    //{
                //    //    string str = lbl.GetType().GetProperty("Name").GetValue(lbl, null).ToString();
                //    //    ReconginizeLbl(str);
                //    //}
                //    lblStr1(lbl,str1);
                //}
                //foreach (Control lbl in this.groupBox2.Controls)
                //{
                //    lblStr1(lbl, str1); 
                //}
                //foreach (Control lbl in this.groupBox3.Controls)
                //{
                //    lblStr1(lbl, str1);
                //}
                //foreach (Control lbl in this.groupBox4.Controls)
                //{
                //    lblStr1(lbl, str1);
                //}            
                #endregion   
            }
            else if (SimulationTextBox.Text == "打开电风扇")
            {
                str1 = "电风扇";
                str2 = "打开";
                foreachLbl(str1, str2);  
            }
            else if (SimulationTextBox.Text == "打开洗衣机")
            {
                str1 = "洗衣机";
                str2 = "打开";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "打开台灯")
            {
                str1 = "台灯";
                str2 = "打开";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "打开空调")
            {
                str1 = "空调";
                str2 = "打开";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "打开微波炉")
            {
                str1 = "微波炉";
                str2 = "打开";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "打开煤气")
            {
                str1 = "煤气";
                str2 = "打开";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "打开电脑")
            {
                str1 = "电脑";
                str2 = "打开";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "打开浴霸")
            {
                str1 = "浴霸";
                str2 = "打开";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "打开吹风机")
            {
                str1 = "吹风机";
                str2 = "打开";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "打开水龙头")
            {
                str1 = "水龙头";
                str2 = "打开";
                foreachLbl(str1,str2);
            }
            else if (SimulationTextBox.Text == "关闭电风扇")
            {
                str1 = "电风扇";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭洗衣机")
            {
                str1 = "洗衣机";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭台灯")
            {
                str1 = "台灯";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭空调")
            {
                str1 = "空调";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭微波炉")
            {
                str1 = "微波炉";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭煤气")
            {
                str1 = "煤气";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭电脑")
            {
                str1 = "电脑";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭浴霸")
            {
                str1 = "浴霸";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭吹风机")
            {
                str1 = "吹风机";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭水龙头")
            {
                str1 = "水龙头";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if (SimulationTextBox.Text == "关闭电视机" || SimulationTextBox.Text == "关闭客厅电视机")
            {
                str1 = "电视机";
                str2 = "关闭";
                foreachLbl(str1, str2);
            }
            else if(SimulationTextBox.Text!="")
            { 
                time.Stop();
                //MessageBox.Show("未能识别到，请重新识别！"); 
                DialogResult r = MessageBox.Show("未能识别到，请重新识别！", "识别错误", MessageBoxButtons.OK);
                if (r == DialogResult.OK)
                {
                    strClear();
                    time.Start();
                }
            }
        }
        /// <summary>
        /// 遍历lbl
        /// </summary>
        /// <param name="str1"></param>
        public void foreachLbl(string str1, string str2)
        {
            foreach (Control lbl in this.groupBox1.Controls)
            {
                lblStr1(lbl, str1, str2);
            }
            foreach (Control lbl in this.groupBox2.Controls)
            {
                lblStr1(lbl, str1, str2);
            }
            foreach (Control lbl in this.groupBox3.Controls)
            {
                lblStr1(lbl, str1, str2);
            }
            foreach (Control lbl in this.groupBox4.Controls)
            {
                lblStr1(lbl, str1, str2);
            } 
 
        }
        /// <summary>
        /// 找相应的lbl
        /// </summary>
        /// <param name="lbl"></param>
        /// <param name="str1"></param>
        public void lblStr1(Control lbl, string str1, string str2)
        { 
            if (lbl.Text == str1 && str2=="打开")
            {
                string str = lbl.GetType().GetProperty("Name").GetValue(lbl, null).ToString();
                ReconginizeLbl(str);
            }
            else if (lbl.Text == str1 && str2 == "关闭")
            {
                string str = lbl.GetType().GetProperty("Name").GetValue(lbl, null).ToString();
                ReconginizeLbl1(str);
            }
        }
        /// <summary>
        /// 判断要打开的家电
        /// </summary>
        /// <param name="str"></param>
        public void ReconginizeLbl(string str)
        {
            switch (str)
            {
                case "labelControl1":
                    {
                        if (pictureEdit1.Visible == true && toggleSwitch1.IsOn == false)
                        {
                            MessageBox.Show(labelControl1.Text + "已经被打开，你可以关闭它！");
                        }
                        else
                        { 
                        pictureEdit1.Show();
                        toggleSwitch1.IsOn = true;
                        }  
                        strClear();
                        break; 
                    }
                case "labelControl2":
                    {
                        if (pictureEdit2.Visible == true && toggleSwitch2.IsOn == false)
                        {
                            MessageBox.Show(labelControl2.Text + "已经被打开，你可以关闭它！");
                        }
                        else
                        {
                            pictureEdit2.Show();
                            toggleSwitch2.IsOn = true;
                        }
                        strClear();
                        break;
                    }
                case "labelControl3":
                    {
                        if (pictureEdit3.Visible == true && toggleSwitch3.IsOn == false)
                        {
                            MessageBox.Show(labelControl3.Text + "已经被打开，你可以关闭它！");
                        }
                        else
                        {
                            pictureEdit3.Show();
                            toggleSwitch3.IsOn = true;
                        }
                        strClear();
                        break;
                    }
                case "labelControl4":
                    {
                        if (pictureEdit4.Visible == true && toggleSwitch4.IsOn == false)
                        {
                            MessageBox.Show(labelControl4.Text + "已经被打开，你可以关闭它！");
                        }
                        else
                        {
                            pictureEdit4.Show();
                            toggleSwitch4.IsOn = true;
                        }
                        strClear();
                        break;
                    }
                case "labelControl5":
                    {
                        if (pictureEdit5.Visible == true && toggleSwitch5.IsOn == false)
                        {
                            MessageBox.Show(labelControl5.Text + "已经被打开，你可以关闭它！");
                        }
                        else
                        {
                            pictureEdit5.Show();
                            toggleSwitch5.IsOn = true;
                        }
                        strClear();
                        break;
                    }
                case "labelControl6":
                    {
                        if (pictureEdit6.Visible == true && toggleSwitch6.IsOn == false)
                        {
                            MessageBox.Show(labelControl6.Text + "已经被打开，你可以关闭它！");
                        }
                        else
                        {
                            pictureEdit6.Show();
                            toggleSwitch6.IsOn = true;
                        }
                        strClear();
                        break;
                    }
                case "labelControl7":
                    {
                        if (pictureEdit7.Visible == true && toggleSwitch7.IsOn == false)
                        {
                            MessageBox.Show(labelControl7.Text + "已经被打开，你可以关闭它！");
                        }
                        else
                        {
                            pictureEdit7.Show();
                            toggleSwitch7.IsOn = true;
                        }
                        strClear();
                        break;
                    }
                case "labelControl8":
                    {
                        if (pictureEdit8.Visible == true && toggleSwitch8.IsOn == false)
                        {
                            MessageBox.Show(labelControl8.Text + "已经被打开，你可以关闭它！");
                        }
                        else
                        {
                            pictureEdit8.Show();
                            toggleSwitch8.IsOn = true;
                        }
                        strClear();
                        break;
                    }
            }
        }
        /// <summary>
        /// 判断要关闭的家电
        /// </summary>
        /// <param name="str"></param>
        public void ReconginizeLbl1(string str)
        {
            switch (str)
            {
                case "labelControl1":
                    {
                        if (pictureEdit1.Visible == false && toggleSwitch1.IsOn == true)
                        {
                            MessageBox.Show(labelControl1.Text + "已经被关闭，你可以打开它！");
                        }
                        else
                        {
                            pictureEdit1.Hide();
                            toggleSwitch1.IsOn = false;
                        }
                        strClear();
                        break;
                    }
                case "labelControl2":
                    {
                        if (pictureEdit2.Visible == false && toggleSwitch2.IsOn == true)
                        {
                            MessageBox.Show(labelControl2.Text + "已经被关闭，你可以打开它！");
                        }
                        else
                        {
                            pictureEdit2.Hide();
                            toggleSwitch2.IsOn = false;
                        }
                        strClear();
                        break;
                    }
                case "labelControl3":
                    {
                        if (pictureEdit3.Visible == false && toggleSwitch3.IsOn == true)
                        {
                            MessageBox.Show(labelControl3.Text + "已经被关闭，你可以打开它！");
                        }
                        else
                        {
                            pictureEdit3.Hide();
                            toggleSwitch3.IsOn = false;
                        }
                        strClear();
                        break;
                    }
                case "labelControl4":
                    {
                        if (pictureEdit4.Visible == false && toggleSwitch4.IsOn == true)
                        {
                            MessageBox.Show(labelControl4.Text + "已经被关闭，你可以打开它！");
                        }
                        else
                        {
                            pictureEdit4.Hide();
                            toggleSwitch4.IsOn = false;
                        }
                        strClear();
                        break;
                    }
                case "labelControl5":
                    {
                        if (pictureEdit5.Visible == false && toggleSwitch5.IsOn == true)
                        {
                            MessageBox.Show(labelControl5.Text + "已经被关闭，你可以打开它！");
                        }
                        else
                        {
                            pictureEdit5.Hide();
                            toggleSwitch5.IsOn = false;
                        }
                        strClear();
                        break;
                    }
                case "labelControl6":
                    {
                        if (pictureEdit6.Visible == false && toggleSwitch6.IsOn == true)
                        {
                            MessageBox.Show(labelControl6.Text + "已经被关闭，你可以打开它！");
                        }
                        else
                        {
                            pictureEdit6.Hide();
                            toggleSwitch6.IsOn = false;
                        }
                        strClear();
                        break;
                    }
                case "labelControl7":
                    {
                        if (pictureEdit7.Visible == false && toggleSwitch7.IsOn == true)
                        {
                            MessageBox.Show(labelControl7.Text + "已经被关闭，你可以打开它！");
                        }
                        else
                        {
                            pictureEdit7.Hide();
                            toggleSwitch7.IsOn = false;
                        }
                        strClear();
                        break;
                    }
                case "labelControl8":
                    {
                        if (pictureEdit8.Visible == false && toggleSwitch8.IsOn == true)
                        {
                            MessageBox.Show(labelControl8.Text + "已经被关闭，你可以打开它！");
                        }
                        else
                        {
                            pictureEdit8.Hide();
                            toggleSwitch8.IsOn = false;
                        }
                        strClear();
                        break;
                    }
            }
        }
        /// <summary>
        /// 清空SimulationTextBox并识别
        /// </summary>
        public void strClear()
        {
            if (SimulationTextBox.Text != null)
            {
                SimulationTextBox.Clear();
            }
            simulationRecongnition.simulationRecongnitionInitialize();
            simulationRecongnition.BeginRec(SimulationTextBox);
        }

        private void CloseSpeechRecongnitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            time.Stop();
        }

        private void SelectSkinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelectSkinForm skinForm = new SelectSkinForm();
            skinForm.Show();
        }
        public void SetSkin(Image img)
        {
            panel1.BackgroundImageLayout = ImageLayout.Stretch;
            panel1.BackgroundImage = img;
        }

    }
}
